﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using JeffWilcox.Utilities.Silverlight;
using System.Windows.Browser;
using System.Threading;
using System.IO;
using System.Text;
using System.Runtime.Serialization.Json;
using System.Reflection;
using VkApi.DataTypes;

namespace VkApi
{
    public partial class VkApi
    {
        /// <summary>
        /// Gets the question list.
        /// </summary>
        /// <param name="uids">The questions user IDs. (unnecessary)</param>
        /// <param name="sort">The sortint key. 1 by date up, 2 by date down. (unnecessary)</param>
        /// <param name="need_profiles">The info length. Values 0-3.</param>
        /// <param name="name_case">The name case. Values: nom,gen,dat,acc,ins,abl. Nom by default. (unnecessary)</param>
        /// <param name="count">The count of results. (unnecessary)</param>
        /// <param name="offset">The offset. (unnecessary)</param>
        /// <param name="callback">The callback.</param>
        /// <returns>Array of Questions.</returns>
        public long Questions_Get(long[] uids, int? sort, int need_profiles, string name_case, int? count, int? offset, ApiCallback<Question[]> callback)
        {
            List<Parameter> list = new List<Parameter>();
            if (uids.Length > 0)
            {
                string uidsString = "";
                foreach (long s in uids)
                    uidsString += s.ToString() + ",";
                uidsString = uidsString.Remove(uidsString.Length - 1);
                list.Add(new Parameter("uids", uidsString)); 
            }
            if (sort != null) list.Add(new Parameter("sort", sort.ToString()));
            list.Add(new Parameter("need_profiles", need_profiles.ToString()));
            if (name_case != null) list.Add(new Parameter("name_case", name_case));
            if (count != null) list.Add(new Parameter("count", count.ToString()));
            if (offset != null) list.Add(new Parameter("offset", offset.ToString()));

            return this.SendRequest("questions.get", callback, list.ToArray());
        }

        /// <summary>
        /// Gets the question list.
        /// </summary>
        /// <param name="qid">The question ID. (unnecessary)</param>
        /// <param name="sort">The sortint key. 1 by date up, 2 by date down. (unnecessary)</param>
        /// <param name="need_profiles">The info length. Values 0-3. (unnecessary)</param>
        /// <param name="name_case">The name case. Values: nom,gen,dat,acc,ins,abl. Nom by default. (unnecessary)</param>
        /// <param name="count">The count of results. (unnecessary)</param>
        /// <param name="offset">The offset. (unnecessary)</param>
        /// <param name="callback">The callback.</param>
        /// <returns>Array of Questions.</returns>
        public long Questions_Get(long qid, int? sort, int need_profiles, string name_case, int? count, int? offset, ApiCallback<Question[]> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("qid", qid.ToString()));
            if (sort != null) list.Add(new Parameter("sort", sort.ToString()));
             list.Add(new Parameter("need_profiles", need_profiles.ToString()));
            if (name_case != null) list.Add(new Parameter("name_case", name_case));
            if (count != null) list.Add(new Parameter("count", count.ToString()));
            if (offset != null) list.Add(new Parameter("offset", offset.ToString()));

            return this.SendRequest("questions.get", callback, list.ToArray());
        }

        /// <summary>
        /// Edits the question.
        /// </summary>
        /// <param name="qid">The question ID.</param>
        /// <param name="text">The new text.</param>
        /// <param name="type">The new type.</param>
        /// <param name="callback">The callback.</param>
        /// <returns>1 if success.</returns>
        public long Questions_Edit(long qid, string text, int type, ApiCallback<int> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("qid", qid.ToString()));
            if (text != null) list.Add(new Parameter("text", text));
            list.Add(new Parameter("type", type.ToString()));
            return this.SendRequest("questions.edit", callback, list.ToArray());
        }

        /// <summary>
        /// Adds the new question of the current user.
        /// </summary>
        /// <param name="text">The new question text.</param>
        /// <param name="type">The new question type.</param>
        /// <param name="callback">The callback.</param>
        /// <returns>New question ID.</returns>
        public long Questions_Add(string text, int type, ApiCallback<long> callback)
        {
            List<Parameter> list = new List<Parameter>();
            if (text != null) list.Add(new Parameter("text", text));
            list.Add(new Parameter("type", type.ToString()));
            return this.SendRequest("questions.add", callback, list.ToArray());
        }

        /// <summary>
        /// Deletes the question.
        /// </summary>
        /// <param name="qid">The question ID.</param>
        /// <param name="callback">The callback.</param>
        /// <returns>1 if success.</returns>
        public long Questions_Delete(long qid, ApiCallback<int> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("qid", qid.ToString()));
            return this.SendRequest("questions.delete", callback, list.ToArray());
        }

        /// <summary>
        /// Searches the questions by some info.
        /// </summary>
        /// <param name="text">The keywords. (unnecessary)</param>
        /// <param name="sort">The sortint key. 1 by date up, 2 by date down. (unnecessary)</param>
        /// <param name="type">The type of the question. (unnecessary)</param>
        /// <param name="need_profiles">The info length. Values 0-3. (unnecessary)</param>
        /// <param name="name_case">The name case. Values: nom,gen,dat,acc,ins,abl. Nom by default. (unnecessary)</param>
        /// <param name="count">The count of results. (unnecessary)</param>
        /// <param name="offset">The offset. (unnecessary)</param>
        /// <param name="callback">The callback.</param>
        /// <returns>Array of Questions.</returns>
        public long Questions_Search(string[] text, int? sort, int? type, int need_profiles, string name_case, int? count, int? offset, ApiCallback<Question[]> callback)
        {
            List<Parameter> list = new List<Parameter>();
            if (text != null)
            {
                string textString = "";
                foreach (string s in text)
                    textString += s.ToString() + ",";
                textString = textString.Remove(textString.Length - 1);
                list.Add(new Parameter("text", textString));
            }
            if (sort != null) list.Add(new Parameter("sort", sort.ToString()));
            if (type != null) list.Add(new Parameter("type", type.ToString()));
            list.Add(new Parameter("need_profiles", need_profiles.ToString()));
            if (name_case != null) list.Add(new Parameter("name_case", name_case));
            if (count != null) list.Add(new Parameter("count", count.ToString()));
            if (offset != null) list.Add(new Parameter("offset", offset.ToString()));

            return this.SendRequest("questions.search", callback, list.ToArray());
        }

        /// <summary>
        /// Gets all types of questions.
        /// </summary>
        /// <param name="callback">The callback.</param>
        /// <returns></returns>
        public long Questions_GetTypes(ApiCallback<QuestionType[]> callback)
        {
            return this.SendRequest("questions.getTypes", callback, null);
        }

        /// <summary>
        /// Gets all answers which user have answered at.
        /// </summary>
        /// <param name="sort">The sortint key. 1 by date up, 2 by date down. (unnecessary)</param>
        /// <param name="need_profiles">The info length. Values 0-3. (unnecessary)</param>
        /// <param name="name_case">The name case. Values: nom,gen,dat,acc,ins,abl. Nom by default. (unnecessary)</param>
        /// <param name="count">The count of results. (unnecessary)</param>
        /// <param name="offset">The offset. (unnecessary)</param>
        /// <param name="callback">The callback.</param>
        /// <returns>Array of Questions.</returns>
        public long Questions_GetOutbound(int? sort, int need_profiles, string name_case, int? count, int? offset, ApiCallback<Question[]> callback)
        {
            List<Parameter> list = new List<Parameter>();
            if (sort != null) list.Add(new Parameter("sort", sort.ToString()));
            list.Add(new Parameter("need_profiles", need_profiles.ToString()));
            if (name_case != null) list.Add(new Parameter("name_case", name_case));
            if (count != null) list.Add(new Parameter("count", count.ToString()));
            if (offset != null) list.Add(new Parameter("offset", offset.ToString()));
            return this.SendRequest("questions.getOutbound", callback, list.ToArray());
        }

        /// <summary>
        /// Gets all answers of the question.
        /// </summary>
        /// <param name="qid">The question ID.</param>
        /// <param name="sort">The sortint key. 1 by date up, 2 by date down. (unnecessary)</param>
        /// <param name="need_profiles">The info length. Values 0-3. (unnecessary)</param>       
        /// <param name="count">The count of results. (unnecessary)</param>
        /// <param name="offset">The offset. (unnecessary)</param>
        /// <param name="callback">The callback.</param>
        /// <returns></returns>
        public long Questions_GetAnswers(long qid, int? sort, int need_profiles, int? count, int? offset, ApiCallback<Answer[]> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("qid", qid.ToString()));
            if (sort != null) list.Add(new Parameter("sort", sort.ToString()));
            list.Add(new Parameter("need_profiles", need_profiles.ToString()));
            if (count != null) list.Add(new Parameter("count", count.ToString()));
            if (offset != null) list.Add(new Parameter("offset", offset.ToString()));
            return this.SendRequest("questions.getAnswers", callback, list.ToArray());
        }

        /// <summary>
        /// Adds the answer to the question.
        /// </summary>
        /// <param name="uid">The user ID.</param>
        /// <param name="qid">The question ID.</param>
        /// <param name="text">The text of the answer.</param>
        /// <param name="callback">The callback.</param>
        /// <returns>1 if success.</returns>
        public long Questions_AddAnswer(long uid, long qid, string text, ApiCallback<int> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("uid", uid.ToString()));
            list.Add(new Parameter("qid", qid.ToString()));
            list.Add(new Parameter("text", text));
            return this.SendRequest("questions.addAnswer", callback, list.ToArray());
        }

        /// <summary>
        /// Deletes the question.
        /// </summary>
        /// <param name="uid">The user ID of question author.</param>
        /// <param name="aid">The answer ID.</param>
        /// <param name="callback">The callback.</param>
        /// <returns>1 if success.</returns>
        public long Questions_DeleteAnswer(long uid, long aid, ApiCallback<int> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("uid", uid.ToString()));
            list.Add(new Parameter("aid", aid.ToString()));
            return this.SendRequest("questions.deleteAnswer", callback, list.ToArray());
        }

        /// <summary>
        /// Joins to the answer.
        /// </summary>
        /// <param name="uid">The user id of question author.</param>
        /// <param name="aid">The answer id which wil be approved.</param>
        /// <param name="callback">The callback.</param>
        /// <returns>New count of users joined to the answer.</returns>
        public long Questions_JoinAnswer(long uid, long aid, ApiCallback<int> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("uid", uid.ToString()));
            list.Add(new Parameter("aid", aid.ToString()));
            return this.SendRequest("questions.joinAnswer", callback, list.ToArray());
        }

        /// <summary>
        /// Gets the list of users that joined to answer.
        /// </summary>
        /// <param name="uid">The user ID of question author.</param>
        /// <param name="aid">The answer ID.</param>
        /// <param name="sort">The sortint key. 1 by date up, 2 by date down. (unnecessary)</param>
        /// <param name="need_profiles">The info length. Values 0-3. (unnecessary)</param>       
        /// <param name="count">The count of results. (unnecessary)</param>
        /// <param name="offset">The offset. (unnecessary)</param>
        /// <param name="callback">The callback.</param>
        /// <returns>Votes object.</returns>
        public long Questions_GetAnswerVotes(long uid, long aid, int? sort, int need_profiles, int? count, int? offset, ApiCallback<Votes> callback)
        {
            List<Parameter> list = new List<Parameter>();
            list.Add(new Parameter("uid", uid.ToString()));
            list.Add(new Parameter("aid", aid.ToString()));
            if (sort != null) list.Add(new Parameter("sort", sort.ToString()));
            list.Add(new Parameter("need_profiles", need_profiles.ToString()));
            if (count != null) list.Add(new Parameter("count", count.ToString()));
            if (offset != null) list.Add(new Parameter("offset", offset.ToString()));
            return this.SendRequest("questions.getAnswerVotes", callback, list.ToArray());
        }

        /// <summary>
        /// Marks the questions as viewed.
        /// </summary>
        /// <param name="aids">The answer IDs. (unnecessary)</param>
        /// <param name="callback">The callback.</param>
        /// <returns>1. If some IDs will not rely on questions they will be ignored.</returns>
        public long Question_MarkAsViewed(long[] aids, ApiCallback<int> callback)
        {
            List<Parameter> list = new List<Parameter>();
            if (aids.Length > 0)
            {
                string aidsString = "";
                foreach (long s in aids)
                    aidsString += s.ToString() + ",";
                aidsString = aidsString.Remove(aidsString.Length - 1);
                list.Add(new Parameter("aids", aidsString));
            }
            return this.SendRequest("questions.markAsViewed", callback, list.ToArray());
        }

    }
}
